package org.zpmis.api.business;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.annotation.Transactional;
import org.zpmis.common.base.AdminLoginUser;
import org.zpmis.common.base.BaseWebService;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.common.result.ResultCode;
import org.zpmis.common.utils.StringUtil;
import org.zpmis.domain.company.model.Company;
import org.zpmis.domain.company.service.CompanyService;
import org.zpmis.domain.customer.enums.CustomerCarePlanEnum;
import org.zpmis.domain.customer.enums.ReturnVisitEnum;
import org.zpmis.domain.customer.model.CustomerCarePlan;
import org.zpmis.domain.customer.model.ReturnVisit;
import org.zpmis.domain.customer.qo.*;
import org.zpmis.domain.customer.service.CustomerCarePlanService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.zpmis.domain.customer.service.ReturnVisitService;
import org.zpmis.domain.customer.vo.CustomerCarePlanListVo;
import org.zpmis.domain.customer.vo.GetCustomerCarePlanDetailVo;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with CodeGenerator
 * Description:
 * @author  Ljx
 * Date: 2021-8-16
 * Time: 13:24:07
 */
@Service
@Slf4j
public class CustomerCarePlanWebService extends BaseWebService {
     @Reference
     private CustomerCarePlanService customerCarePlanService;

     @Reference
     private CompanyService companyService;


     /**
      * 新增意向客户关怀计划
      * @param addCustomerCarePlanQo
      * @throws BusinessException
      * @throws Exception
      */
     @Transactional
     public void addCustomerCarePlan(AddCustomerCarePlanQo addCustomerCarePlanQo) throws BusinessException,Exception{

          //获取当前登录用户
          AdminLoginUser adminLoginUser = super.getAdminLoginUser();
          if(adminLoginUser == null){
               throw new BusinessException(ResultCode.USER_NOT_LOGIN);
          }
          Long companyId = addCustomerCarePlanQo.getCompanyId();
          Company company = companyService.findById(companyId);
          if(company == null){
               throw new BusinessException(ResultCode.DATA_NOT_FOUND);
          }
          //新增计划信息
          customerCarePlanService.save(
                  CustomerCarePlan.builder()
                          .careObject(CustomerCarePlanEnum.CARE_OBJECT_INTENTION.code())
                          .chargePersonId(addCustomerCarePlanQo.getChargePersonId())
                          .chargePerson(addCustomerCarePlanQo.getChargePerson())
                          .companyName(addCustomerCarePlanQo.getCompanyName())
                          .contactPerson(addCustomerCarePlanQo.getContactPerson())
                          .contactMobile(addCustomerCarePlanQo.getContactMobile())
                          .planDesc(addCustomerCarePlanQo.getPlanDesc())
                          .careType(addCustomerCarePlanQo.getCareType())
                          .careTime(addCustomerCarePlanQo.getCareTime())
                          .isDeleted(CustomerCarePlanEnum.NOT_DELETE.code())
                          .status(CustomerCarePlanEnum.DISABLE_STATUS.code())
                          .isRemind(addCustomerCarePlanQo.getIsRemind() == null ? CustomerCarePlanEnum.REMIND_YES.code() : addCustomerCarePlanQo.getIsRemind())
                          .remake(addCustomerCarePlanQo.getRemake())
                          .createId(adminLoginUser.getId())
                          .createPerson(adminLoginUser.getUserName())
                          .createTime(Instant.now().toEpochMilli())
                          .companyCode(company.getCompanyCode())
                          .companyId(company.getId())
                          .build()
          );

     }


     /**
      * 关怀计划列表查询
      * @param planListQo
      * @return
      */
     public PageInfo planList(CustomerCarePlanListQo planListQo) throws Exception{
          String chargePerson = planListQo.getChargePerson();
          String companyName = planListQo.getCompanyName();
          String contactPerson = planListQo.getContactPerson();
          String contactMobile = planListQo.getContactMobile();
          Integer status = planListQo.getStatus();
          Long startTime = planListQo.getStartTime();
          Long endTime = planListQo.getEndTime();
          Integer careType = planListQo.getCareType();
          Integer careObject = planListQo.getCareObject();

          Condition condition = new Condition(CustomerCarePlan.class);
          Example.Criteria criteria = condition.createCriteria();
          criteria.andEqualTo("isDeleted",CustomerCarePlanEnum.NOT_DELETE.code());
          if(StringUtil.isNotEmpty(chargePerson)){
               criteria.andLike("chargePerson","%"+chargePerson+"%");
          }
          if(StringUtil.isNotEmpty(companyName)){
               criteria.andLike("companyName","%"+companyName+"%");
          }
          if(StringUtil.isNotEmpty(contactPerson)){
               criteria.andLike("contactPerson","%"+contactPerson+"%");
          }
          if(StringUtil.isNotEmpty(contactMobile)){
               criteria.andLike("contactMobile","%"+contactMobile+"%");
          }
          if(careObject != null){
               criteria.andEqualTo("careObject",careObject);
          }
          if(status != null){
               criteria.andEqualTo("status",status);
          }
          if(careType != null){
               criteria.andEqualTo("careType",careType);
          }
          if(startTime != null && endTime != null){
               criteria.andGreaterThanOrEqualTo("createTime",startTime);
               criteria.andLessThanOrEqualTo("createTime",endTime);
          }
          if(startTime != null && endTime == null){
               criteria.andGreaterThanOrEqualTo("createTime",startTime);
               criteria.andLessThanOrEqualTo("createTime",Instant.now().toEpochMilli());
          }
          if(startTime == null && endTime != null){
               criteria.andLessThanOrEqualTo("createTime",endTime);
          }
          condition.orderBy("createTime").desc();
          PageHelper.startPage(planListQo.getPageNum(),planListQo.getPageSize());
          List<CustomerCarePlan> customerCarePlanList = customerCarePlanService.findByCondition(condition);
          PageInfo pageInfo = new PageInfo<>(customerCarePlanList);
          //设置自定义返回对象
          List<CustomerCarePlanListVo> resultList = customerCarePlanList.stream().map(CustomerCarePlanListVo::new).collect(Collectors.toList());
          pageInfo.setList(resultList);
          return pageInfo;
     }

     /**
      * 修改关怀计划
      * @param editCustomerCarePlanQo
      */
     public void editCustomerCarePlan(EditCustomerCarePlanQo editCustomerCarePlanQo){
          Long id = editCustomerCarePlanQo.getId();
          CustomerCarePlan customerCarePlan = customerCarePlanService.findById(id);
          if(customerCarePlan == null){
               throw new BusinessException(ResultCode.DATA_NOT_FOUND);
          }
          customerCarePlan.setChargePersonId(editCustomerCarePlanQo.getChargePersonId());
          customerCarePlan.setChargePerson(editCustomerCarePlanQo.getChargePerson());
          customerCarePlan.setCompanyName(editCustomerCarePlanQo.getCompanyName());
          customerCarePlan.setContactPerson(editCustomerCarePlanQo.getContactPerson());
          customerCarePlan.setContactMobile(editCustomerCarePlanQo.getContactMobile());
          customerCarePlan.setPlanDesc(editCustomerCarePlanQo.getPlanDesc());
          customerCarePlan.setCareType(editCustomerCarePlanQo.getCareType());
          customerCarePlan.setCareTime(editCustomerCarePlanQo.getCareTime());
          customerCarePlan.setStatus(editCustomerCarePlanQo.getStatus());
          customerCarePlan.setIsRemind(editCustomerCarePlanQo.getIsRemind());
          customerCarePlan.setRemake(editCustomerCarePlanQo.getRemake());
          customerCarePlanService.update(customerCarePlan);
     }

     /**
      * 获取计划详情
      * @param getCustomerCarePlanDetailQo
      * @return
      * @throws BusinessException
      * @throws Exception
      */
     public GetCustomerCarePlanDetailVo getCustomerCarePlanDetail(GetCustomerCarePlanDetailQo getCustomerCarePlanDetailQo) throws BusinessException,Exception{
          Long id = getCustomerCarePlanDetailQo.getId();
          CustomerCarePlan customerCarePlan = customerCarePlanService.findById(id);
          if(customerCarePlan == null){
               throw new BusinessException(ResultCode.DATA_NOT_FOUND);
          }
          GetCustomerCarePlanDetailVo carePlanDetail = GetCustomerCarePlanDetailVo.toCustomerCarePlanDetailVo(customerCarePlan);
          return carePlanDetail;
     }

     /**
      * 执行关怀计划
      * @param executeCustomerCarePlanQo
      * @throws BusinessException
      * @throws Exception
      */
     public void executeCustomerCarePlan(ExecuteCustomerCarePlanQo executeCustomerCarePlanQo) throws BusinessException,Exception{
          Long id = executeCustomerCarePlanQo.getId();
          CustomerCarePlan customerCarePlan = customerCarePlanService.findById(id);
          if(customerCarePlan == null){
               throw new BusinessException(ResultCode.DATA_NOT_FOUND);
          }
          //状态为完结 不能再次执行
          if(customerCarePlan.getStatus().equals(CustomerCarePlanEnum.ENABLE_STATUS.code())){
               throw new BusinessException(ResultCode.CARE_PLAN_ALREADY_EXECUTE);
          }
          customerCarePlan.setStatus(CustomerCarePlanEnum.ENABLE_STATUS.code());
          customerCarePlan.setExecuteDesc(executeCustomerCarePlanQo.getExecuteDesc());
          customerCarePlan.setExecuteId(executeCustomerCarePlanQo.getExecuteId());
          customerCarePlan.setExecuteTime(executeCustomerCarePlanQo.getExecuteTime());
          customerCarePlanService.update(customerCarePlan);
     }


     /**
      * 删除客户关怀计划
      * @param deleteCusomerCarePlanQo
      */
     public void deleteCustomerCarePlan(DeleteCusomerCarePlanQo deleteCusomerCarePlanQo) throws BusinessException,Exception{
          Long id = deleteCusomerCarePlanQo.getId();
          CustomerCarePlan customerCarePlan = customerCarePlanService.findById(id);
          if(customerCarePlan == null){
               throw new BusinessException(ResultCode.DATA_NOT_FOUND);
          }
          //状态为完结 不能进行删除
          if(customerCarePlan.getStatus().equals(CustomerCarePlanEnum.ENABLE_STATUS.code())){
               throw new BusinessException(ResultCode.CARE_PLAN_ALREADY_FINISH);
          }
          //修改计划is_delete状态
          customerCarePlan.setIsDeleted(CustomerCarePlanEnum.DELETED.code());
          customerCarePlanService.update(customerCarePlan);
     }
}
